﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.Model.基础;
using LibrarySolution.DAO.基础;
using LibrarySolution.ComMethod;

namespace LibrarySolution.Frm
{
    public partial class FrmTsgh : Form
    {
        #region 属性
        YhDao yhDao;
        JHBkDao jhbkDao;
        JSRecord jsEntity;
        YhxxEntity yhxxEntity;
        private static FrmTsgh Instance;
        #endregion

        #region 构造器
        public FrmTsgh()
        {
            InitializeComponent();
        }
        #endregion

        #region 事件
        public static FrmTsgh GetInstance()
        {
            if (Instance == null || Instance.IsDisposed)
            {
                Instance = new FrmTsgh();
                Instance.MdiParent = GlobalVar.frmMain;
                GlobalVar.frmMain.panel1.Controls.Add(Instance);
                Instance.BringToFront();
            }
            return Instance;
        }

        private void BtnChange_Click(object sender, EventArgs e)
        {
            String yhNo;
            yhDao = new YhDao();
            jhbkDao = new JHBkDao();
            jsEntity = new JSRecord();
            yhxxEntity = new YhxxEntity();
            yhNo = this.TxtQueryByYhNo.Text;
            jsEntity.js_yhno = yhNo;
            yhxxEntity = yhDao.getYhxxByNo(yhNo, "1");
            if (yhxxEntity.yh_no != null && yhxxEntity.yh_code != null)
            {
                refreshDTGV(yhNo);
            }
            else
            {
                MessageBox.Show("用户不存在");
                dataGridView1.DataSource = null;
            }
            TxtQueryByYhNo.Text = "";
        }

        private void BtnTsgh_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0) return;
            if (MessageBox.Show("确定用户已还此书?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                jsEntity = new JSRecord();
                jsEntity.js_code = dataGridView1.SelectedRows[0].Cells["记录编码"].Value.ToString();
                jsEntity.js_tsbh = dataGridView1.SelectedRows[0].Cells["书本编号"].Value.ToString();
                jsEntity.js_yhno = dataGridView1.SelectedRows[0].Cells["用户编号"].Value.ToString();
                jsEntity.js_shrq = DateTime.Now.ToString("yyyy-MM-dd");
                jhbkDao.changeJs(jsEntity);
                refreshDTGV(jsEntity.js_yhno);
                if (dataGridView1.Rows.Count == 0) { this.Close(); }
            }
        }
        #endregion

        #region 方法
        void refreshDTGV(String yhNo)
        {
            DataTable dt;
            jsEntity = new JSRecord();
            jsEntity.js_yhno = yhNo;
            jsEntity.js_state = "false";
            dt = jhbkDao.getGhJl(jsEntity);
            dataGridView1.DataSource = dt;
            dataGridView1.Columns["记录编码"].Visible = false;
            dataGridView1.Refresh();
        }
        #endregion
    }
}
